import { ref } from 'vue';
import { defineStore } from 'pinia';
import ObjectUtils from '@/utils/ObjectUtils';
import { arrayRandom } from '../utils/Utils';
import { useSsqDataSourceStore } from '@/stores/SsqDataSource';
import ssqconfig from '@/config/ssq-config';

const { split_index } = ssqconfig;

const ssqDS = useSsqDataSourceStore();
const statDrawal = ssqDS.getDrawalStat();

/**
 * 过滤状态管理
 */
export const useSsqFilterStateStore = defineStore('SsqFilterState', () => {

    /**
     * 7 个号数选号池数据
     */
    const poolColumns = ref(['', '', '', '', '', '', '']);

    /**
     * 综合选号池数据
     */
    const poolInputText = ref('');

    /**
     * 高级筛选表单数据
     */
    const filterForm = ref({});

    /**
     * 机选一注号码
     */
    const chooseNums = ref([], []);

    /**
     * 设置机选一注号码
     * @param {Array} nums 前区号码和后区号码，示例：[['01', ...], ['01', ...]]
     */
    const setChooseNums = (nums) => {
        chooseNums.value = nums;
    }

    /**
     * 返回机选一注号码
     * @returns {Array}
     */
    const getChooseNums = () => {
        return chooseNums.value;
    }

    /**
     * 机选一注
     * @param {Array} frontData 前区号码池
     * @param {Array} backData 后区号码池
     */
    const getRandomNums7 = (frontData, backData) => {
        if (!frontData || !backData) {
            if (poolInputText.value == '') {
                frontData = ObjectUtils.statObjectToArr(statDrawal.statAll[0], false, { op: '>', value: 3 }).sort();
                backData = ObjectUtils.statObjectToArr(statDrawal.statAll[1], false, { op: '>', value: 3 }).sort();
            } else {
                let arr = poolInputText.value.split(',');
                frontData = arr[0].split(' ').sort();
                backData = arr[1].split(' ').sort();
            }
        }
        return [arrayRandom(frontData, split_index).sort(), arrayRandom(backData, 1).sort()];
    }

    return {
        setChooseNums,
        getChooseNums,
        getRandomNums7,
    }
})
